package logger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @version 1.0
 * @author:罗苏琴
 * @Date 2024/12/11 下午4:47
 */
public class LoggingInvocationHandler {
    public static void beforeMethod(Method method) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String methodName = method.getName();
        System.out.println("[LOG]Method:" + methodName + " started at: " + sdf.format(new Date()));
    }

    public static void afterMethod(Method method, long startTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String methodName = method.getName();
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        System.out.println("[LOG]Method: " + methodName + " ended at " + sdf.format(new Date()) + ", executed in: " + executionTime + " ms");
    }

}
